package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.payconfig.EntityPackageType;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.callpackage.DeleteConfigRequest;
import com.ocom.common.request.callpackage.OnShowRequest;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoPackageType;
import com.ocom.payconfig.service.PackageTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PackageTypeServiceImpl extends ServiceImpl<IDaoPackageType, EntityPackageType> implements PackageTypeService {

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityPackageType> getPackageTypeList(Long companyId, OnShowRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());
        QueryWrapper<EntityPackageType> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id",companyId);
        if (null != request.getPackageTypeId()) {
            queryWrapper.eq("package_type_id", request.getPackageTypeId());
        }
        if (StringUtils.isNotBlank(request.getPackageTypeName())) {
            queryWrapper.like("package_type_name", request.getPackageTypeName());
        }
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean addCallPackageConfig(Long companyId, EntityPackageType request) {
        return this.save(request);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean editCallPackageConfig(Long companyId, EntityPackageType request) {
        return this.updateById(request);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean deleteCallPackageConfig(Long companyId, DeleteConfigRequest request) {
        QueryWrapper<EntityPackageType> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("package_type_id", request.getPackageTypeId());
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityPackageType findByPackageTypeId(Long companyId, Long packageTypeId) {
        QueryWrapper<EntityPackageType> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("package_type_id", packageTypeId);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
}
